package com.example.yiqinmanagersystem.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yiqinmanagersystem.entity.BanJi;
import com.example.yiqinmanagersystem.service.BanjiService;
import com.example.yiqinmanagersystem.vo.BanJiVo;
import com.example.yiqinmanagersystem.vo.ResponseCode;
import com.example.yiqinmanagersystem.vo.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 **@Author:youpeng
 **Created on 2022/9/18
 */
@RestController
@RequestMapping("/api")
public class BanJiController {

    @Resource
    private BanjiService banjiService;

    /*
    *获取班级数据 带条件查询
     */
    @GetMapping("/banji/listBanJi")
    public ResponseResult listBanJi(BanJiVo banJiVo){
        //1.条件
        IPage<BanJi> page = new Page<>(banJiVo.getPage(),banJiVo.getLimit());
        QueryWrapper<BanJi> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(banJiVo.getName()),"name",banJiVo.getName());
        //2.查库
        IPage<BanJi> banJiIPage = banjiService.page(page, queryWrapper);
        if(banJiIPage!=null){
            return new ResponseResult(ResponseCode.SUCCESS, Math.toIntExact(banJiIPage.getTotal()),banJiIPage.getRecords());
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 删除班级 id
    * */
    @DeleteMapping("/banji/deleteById")
    public ResponseResult deleteById(Integer id){
        boolean b = banjiService.removeById(id);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 新增（修改） 班级
    * */
    @PostMapping("/banji/addOrUpdateBanJi")
    public ResponseResult addOrUpdateBanJi(BanJi banJi){
        boolean b = banjiService.saveOrUpdate(banJi);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

}
